<?php
namespace app\index\controller\project;

use app\index\controller\Base;

class ProjectRisk extends base_project
{
    public $tableName = "ProjectRisk";
    public function index()
    {
        $pageData=$this->page(["project_id"=>$this->base_project_id],12,"","id  desc","project,user");
       $data_list=collection($pageData["data_list"])->toArray();
        $assist_uidsData=[];
        $s=   str_replace("\u0000*\u0000","",json_encode($data_list));
      $data_list=json_decode($s,true)["items"];
        $user_list= model("Admin")->field('id,username,nickname')->select()->toArray();
        $user_items=array_column($user_list,"nickname","id");
       foreach($data_list as $key=>$uitem){
        $data_list[$key]["assist_username"]="";
        foreach($user_list as $key1=>$uitem1){
            $strs=",".$uitem["assist_uids"].",";             
            if(!empty(strstr($strs,strval($uitem1["id"])))){             
                $data_list[$key]["assist_username"]=     $data_list[$key]["assist_username"].",".$uitem1["nickname"];               
            }         
        }
        $data_list[$key]["assist_username"]=rtrim(ltrim(   $data_list[$key]["assist_username"],","),",");
       }     
  
     // print_r($data_list);
      
        $risk_type = model("CodeMap")->get_value("risk_type");
        $risk_status = model("CodeMap")->get_value("risk_status");
        $data["data_list"]=$data_list;
        $data["user_items"]=$user_items;
       
        $data["risk_type"] = array_column($risk_type,"m_name","m_value");
        $data["risk_status"] =array_column($risk_status,"m_name","m_value");
        $data["assist_uidsData"] =$assist_uidsData;
        $this->assign($data);
        return view();
    }

    /**
     *  添加
     */
    public function add()
    {
        $ProjectRiskModel = model("ProjectRisk");
        if (request()->instance()->isPost()) {
            $post = request()->instance()->param();
        
            $data["project_id"] = $post["project_id"];
            $data["plan_id"] = $post["plan_id"];
            $data["mtype"] = $post["mtype"];
            $data["content"] = $post["content"];
            $data["solution"] = $post["solution"];
            $data["status"] = $post["status"];
            $data["solve_time"] = $post["solve_time"];
            $data["real_solve_time"] = $post["real_solve_time"];
            $data["uid"] = $this->member_id;
            $data["assist_uids"] = implode(",",$post["assist_uids"]);
            $data["remark"] = $post["remark"];   
        
           
            $result = $ProjectRiskModel->save($data);
            if (empty($result)) {
                $this->error("操作失败");

            }
        
            $this->success("操作成功", url("/Index/Project/ProjectRisk/index"));
        }
        $project_id = $this->base_project_id;
        $data["plan_list"] =   model("ProjectPlan")->where(["project_id"=>$this->base_project_id])->field('id,title')->select();
        $data["risk_type"] = model("CodeMap")->get_value("risk_type");
        $data["risk_status"] = model("CodeMap")->get_value("risk_status");
        $data["user_list"]= model("Admin")->all();
        $this->assign($data);
        return view();
    }

    /**
     *  修改
     */
    public function edit()
    {
        $ProjectRiskModel = model('ProjectRisk');
        if (request()->instance()->isPost()) {
            $post = request()->instance()->param();           
            $data["plan_id"] = $post["plan_id"];
            $data["mtype"] = $post["mtype"];
            $data["content"] = $post["content"];
            $data["solution"] = $post["solution"];
            $data["status"] = $post["status"];
            $data["solve_time"] = $post["solve_time"];
            $data["real_solve_time"] = $post["real_solve_time"];
            $data["uid"] = $this->member_id;
            $data["assist_uids"] = $post["assist_uids"];
            $data["remark"] = $post["remark"];
       

            $map["id"] = $post["id"];
            $result = $ProjectRiskModel->save($data, $map);
            if (empty($result)) {
                $this->error("操作失败");

            }
            $this->success("操作成功", url("/Index/Project/ProjectRisk/index"));
        }

        $id = input("id");
        $data["plan_list"] =   model("ProjectPlan")->where(["project_id"=>$this->base_project_id])->field('id,title')->select();
     
        $data["info"] = $ProjectRiskModel->get($id);
        $data["risk_type"] = model("CodeMap")->get_value("risk_type");
        $data["risk_status"] = model("CodeMap")->get_value("risk_status");
        $data["user_list"]= model("Admin")->all();
        $this->assign($data);
        return view();
    }

    public function del()
    {
        $id = input("id");
        if (empty($id)) {
            $this->error("删除失败");
        }
        $ProjectRiskModel = model('ProjectRisk');
        $result = $ProjectRiskModel->where('id', intval($id))->delete();
        if ($result) {
            $this->success("删除成功");
        }
        $this->error("删除失败");
    }


    public function done(){
        $id=input("id");
        if (empty($id)) {
            $this->error("操作失败");
        }
        $project_id=$this->base_project_id;
       $count= model("ProjectRiskinfo")->where(["project_id"=>$project_id,"plan_id"=>$id])->count();
       if($count>0){
        $this->error("请先处理风险详情");
       }
       $data["status"]=3;
        $data["real_solve_time"]=date("Y-m-d");
       $result=model('ProjectRisk')->where(["id"=>$id,"project_id"=>$project_id])->update( $data);
       if ($result) {
        $this->success("操作成功");
    }
    $this->error("操作失败");
    }

    public  function stop(){
        $id=input("id");
        if (empty($id)) {
            $this->error("操作失败");
        }
        $project_id=$this->base_project_id;
        $data["status"]=2;
        $data["real_solve_time"]=date("Y-m-d");
        $result=model('ProjectRisk')->where(["id"=>$id,"project_id"=>$project_id])->update($data);
        if ($result) {
            $this->success("操作成功");
        }
    }

}
